<template>
  <transition name="slide-down">
    <div v-if="latestUnreadNotification" class="notification-bar">
      <div class="notification-content">
        <div class="notification-info">
          <el-icon class="notification-icon"><Bell /></el-icon>
          <div class="notification-text">
            <span class="notification-title">{{ latestUnreadNotification.title }}:</span>
            <span class="notification-content-text">{{ latestUnreadNotification.content }}</span>
          </div>
        </div>
        <el-button type="primary" link @click="closeNotification" class="close-button">
          <el-icon><Close /></el-icon>
        </el-button>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useSystemNotificationStore } from '@/stores/systemNotification'
import { Bell, Close } from '@element-plus/icons-vue'
import websocket, { WsRequestType } from '@/utils/websocket'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const systemNotificationStore = useSystemNotificationStore()
const router = useRouter()

// 获取最新的未读通知
const latestUnreadNotification = computed(() => {
  const unreadNotifications = systemNotificationStore.notifications.filter(
    notification => !systemNotificationStore.isNotificationRead(notification)
  )
  
  // 返回最新的一条未读通知
  if (unreadNotifications.length > 0) {
    return unreadNotifications[0]
  }
  return null
})

// 关闭通知（标记为已读）
const closeNotification = () => {
  if (!latestUnreadNotification.value) return
  
  // 发送WebSocket消息标记为已读
  const message = {
    type: WsRequestType.SYSTEM_NOTIFICATION_READ,
    notificationId: latestUnreadNotification.value.notification_id
  }
  websocket.sendMessage(message)
}
</script>

<style scoped>
.notification-bar {
  height: 40px;
  background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  z-index: 1001;
  display: flex;
  align-items: center;
}

.notification-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.notification-info {
  display: flex;
  align-items: center;
  flex: 1;
  overflow: hidden;
}

.notification-icon {
  font-size: 16px;
  margin-right: 10px;
  color: #fff;
  flex-shrink: 0;
}

.notification-text {
  color: #fff;
  font-weight: 500;
  font-size: 14px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.notification-title {
  font-weight: bold;
  margin-right: 8px;
}

.notification-content-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: normal;
  opacity: 0.9;
}

.close-button {
  color: #fff !important;
  padding: 0;
  font-size: 16px;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-left: 15px;
}

.close-button:hover {
  color: #f0f0f0 !important;
  background: rgba(255, 255, 255, 0.1) !important;
  border-radius: 50%;
}

.slide-down-enter-active,
.slide-down-leave-active {
  transition: all 0.3s ease;
}

.slide-down-enter-from,
.slide-down-leave-to {
  opacity: 0;
  transform: translateY(-100%);
}
</style>